<?php
require_once '../classes/includeAll.php';
$title="User";
$head ='<script src="../javascript/pagesJavascript/user.js" type="text/javascript"></script>'.
       '<link rel="stylesheet" type="text/css" href="../design/css/users.css">';


$id = filter_input(INPUT_GET,"id");

if(!is_numeric($id))
{
  $id=$sessionId;
}



require '../menu/head.php';

if(!($isAdmin || $sessionId==$id))
{
  echo 'Unautorized Access!';
  require '../menu/footer.php';
  exit();
}

require '../resources/getConnection.php';
$userClass = new users($connection);

//Auswertung

if(isset($_POST["submit"]))
{
  $name = filter_input(INPUT_POST, "name");
  $pw1=  filter_input(INPUT_POST, "password1");
  $pw2=filter_input(INPUT_POST,"password2");
  $rights = filter_input(INPUT_POST, "rights",FILTER_VALIDATE_INT);
  $email = filter_input(INPUT_POST, "email",FILTER_VALIDATE_EMAIL);
  
  $column = array();
  $argument = array();
  
  if($name != null && $name != false)
  {
    array_push($column, "name");
    array_push($argument, $name);
  }
  
  if($pw1 != null && $pw1 != false && $pw2 != null && $pw2 != false && $pw1 == $pw2)
  {
    array_push($column, "password");
    array_push($argument, EncryptPw($pw1));
  }
  
  if($rights != null && $rights != false && $rights < 3 && $rights >= 1)
  {
    $rights--;
    array_push($column, "rights");
    array_push($argument, $rights);
  }
  
  if($email != null && $email != false)
  {    
    array_push($column, "email");
    array_push($argument, $email);
  }
  //echo print_r($column);
  //echo print_r($argument);
  echo $userClass->UpdateUser($column,$argument,$id);
}

$user = $userClass->GetUser($id);
  $connection->disconnectDB();
echo '<h1>Benutzer Konto</h1>';
echo '<form action="user.php?id='.$id.'&sessionid='.$sessionId.'" method="POST">
<table class="userTable">

<tbody>';
echo "<tr><td><span class=\"fontOne\">Username:";
if($isAdmin){echo "*";}
echo"</span></td><td";
if($isAdmin){
  echo " onclick='insertTextField(this,\"name\",0)'"; }
echo ">".$user[1].'</td></tr>';
echo "<tr><td><span class=\"fontOne\">Passwort:*</span></td><td onclick='insertTextField(this,\"password\",1)'>***</td></tr>";
echo "<tr><td><span class=\"fontOne\">Rechte:";
if($userClass->AllowChangeRights){echo "*";}
echo "</span></td><td";
if($userClass->AllowChangeRights){
  echo " onclick='CreateList(this,\"rights\",2)'"; }
echo ">".$user[2].'</td></tr>';
echo "<tr><td><span class=\"fontOne\">E-mail:*</span></td><td onclick='insertTextField(this,\"email\",3)'>".$user[3].'</td></tr>';
echo '</tbody></table>';
echo '<p id="submitArea"></p></form>';
require '../menu/footer.php';
?>

